<template>
  <div class="departments-page">
    <el-card>
      <template #header>
        <div class="page-header">
          <h3>部门管理</h3>
          <el-button type="primary" @click="showAddDialog">
            <el-icon><Plus /></el-icon>
            新增部门
          </el-button>
        </div>
      </template>

      <el-table :data="tableData" v-loading="loading" stripe>
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="部门名称" width="150" />
        <el-table-column prop="code" label="部门编码" width="150" />
        <el-table-column prop="parent" label="上级部门" width="150" />
        <el-table-column prop="manager" label="部门负责人" width="120" />
        <el-table-column prop="phone" label="联系电话" width="130" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === '启用' ? 'success' : 'danger'">
              {{ row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="danger" size="small" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const loading = ref(false)

const tableData = ref([
  {
    id: 1,
    name: '技术部',
    code: 'TECH',
    parent: '-',
    manager: '张三',
    phone: '13800138000',
    description: '负责技术开发',
    status: '启用'
  },
  {
    id: 2,
    name: '销售部',
    code: 'SALES',
    parent: '-',
    manager: '李四',
    phone: '13800138001',
    description: '负责销售业务',
    status: '启用'
  },
  {
    id: 3,
    name: '财务部',
    code: 'FINANCE',
    parent: '-',
    manager: '王五',
    phone: '13800138002',
    description: '负责财务管理',
    status: '启用'
  }
])

const showAddDialog = () => {
  ElMessage.info('新增部门功能开发中...')
}

const handleEdit = (row) => {
  ElMessage.info('编辑部门功能开发中...')
}

const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除该部门吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    ElMessage.success('删除成功')
  } catch {
    // 用户取消删除
  }
}

onMounted(() => {
  loading.value = true
  setTimeout(() => {
    loading.value = false
  }, 500)
})
</script>

<style scoped>
.departments-page {
  padding: 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.page-header h3 {
  margin: 0;
  color: #303133;
}
</style>
